package com.huanzhidadi.jsoup.util;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;

@Slf4j
public class HtmlParseUtil {

    public static void main(String[] args) throws IOException {
        // 解析url地址,后面的1000ms指解析时间
        Document document = Jsoup.parse(new URL("https://search.jd.com/Search?keyword=编程&ev=3047_55654"), 5000);

        Element element = document.getElementById("J_goodsList");
        Elements elements = element.getElementsByTag("li");
        // Elements elements = document.select(".article-title > a");
        for (Element el : elements) {
            String img = el.getElementsByTag("img").attr("data-lazy-img");
            String price = el.select(".p-price > strong > i").eq(0).text();
            String name = el.getElementsByClass("p-name").eq(0).text();
            log.info("name: {}, price: {}, img: {}", name, price, img);
        }

    }
}
